package com.inei.practicadecampo.model.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.inei.practicadecampo.model.connection.UtilDB;
import com.inei.practicadecampo.model.entity.Respuesta;

public class RespuestaDao implements ICRUD<Respuesta> {

	UtilDB util;
	ContentValues cv;
	Cursor c;
	SQLiteDatabase db;
	Context context;
	Exception e;

	public RespuestaDao(Context context) {
		this.context = context;
		util = new UtilDB(context);
	}

	@Override
	public Respuesta buscar(Respuesta o) throws Exception {
		// TODO Auto-generated method stube = null;
		e = null;
		db = util.getReadableDatabase();
		
		Respuesta r = null;
		
		c = db.rawQuery("select * from respuesta where respuesta_id=?", new String[]{""+o.getRespuesta_id()});
		if(c.moveToFirst()){
				r = new Respuesta();
				r.setRespuesta_id(c.getInt(c.getColumnIndex("respuesta_id")));
				r.setApe_paterno(c.getString(c.getColumnIndex("ape_paterno")));
				r.setApe_materno(c.getString(c.getColumnIndex("ape_materno")));
				r.setNombre(c.getString(c.getColumnIndex("nombre")));
				r.setDni(c.getString(c.getColumnIndex("dni")));
				r.setApe_paterno2(c.getString(c.getColumnIndex("ape_paterno2")));
				r.setApe_materno2(c.getString(c.getColumnIndex("ape_materno2")));
				r.setNombre2(c.getString(c.getColumnIndex("nombre2")));
				r.setDni2(c.getString(c.getColumnIndex("dni2")));
				r.setP01(c.getInt(c.getColumnIndex("P01")));
				r.setP02(c.getInt(c.getColumnIndex("P02")));
				r.setP03(c.getInt(c.getColumnIndex("P03")));
				r.setP04(c.getInt(c.getColumnIndex("P04")));
				r.setP05(c.getInt(c.getColumnIndex("P05")));
				r.setP06(c.getInt(c.getColumnIndex("P06")));
				r.setP07(c.getInt(c.getColumnIndex("P07")));
				r.setP08(c.getInt(c.getColumnIndex("P08")));
				r.setP09(c.getInt(c.getColumnIndex("P09")));
				r.setP10(c.getInt(c.getColumnIndex("P10")));
				r.setNota(c.getDouble(c.getColumnIndex("nota")));
		}else{
			e = new Exception("No hay resultados para su busqueda");
		}
		
		cerrar();
		if(e!=null){
			throw e;
		}
		return r;
	}

	@Override
	public List<Respuesta> leer() throws Exception {
		// TODO Auto-generated method stub
		e = null;
		db = util.getReadableDatabase();
		
		List<Respuesta> lista = null;
		
		c = db.rawQuery("select * from respuesta", null);
		if(c.moveToFirst()){
			lista = new ArrayList<Respuesta>();
			do{
				Respuesta r = new Respuesta();
				r.setRespuesta_id(c.getInt(c.getColumnIndex("respuesta_id")));
				r.setApe_paterno(c.getString(c.getColumnIndex("ape_paterno")));
				r.setApe_materno(c.getString(c.getColumnIndex("ape_materno")));
				r.setNombre(c.getString(c.getColumnIndex("nombre")));
				r.setDni(c.getString(c.getColumnIndex("dni")));
				r.setApe_paterno2(c.getString(c.getColumnIndex("ape_paterno2")));
				r.setApe_materno2(c.getString(c.getColumnIndex("ape_materno2")));
				r.setNombre2(c.getString(c.getColumnIndex("nombre2")));
				r.setDni2(c.getString(c.getColumnIndex("dni2")));
				r.setP01(c.getInt(c.getColumnIndex("P01")));
				r.setP02(c.getInt(c.getColumnIndex("P02")));
				r.setP03(c.getInt(c.getColumnIndex("P03")));
				r.setP04(c.getInt(c.getColumnIndex("P04")));
				r.setP05(c.getInt(c.getColumnIndex("P05")));
				r.setP06(c.getInt(c.getColumnIndex("P06")));
				r.setP07(c.getInt(c.getColumnIndex("P07")));
				r.setP08(c.getInt(c.getColumnIndex("P08")));
				r.setP09(c.getInt(c.getColumnIndex("P09")));
				r.setP10(c.getInt(c.getColumnIndex("P10")));
				r.setNota(c.getDouble(c.getColumnIndex("nota")));
				lista.add(r);
			}while(c.moveToNext());
		}else{
			e = new Exception("No hay registros");
		}
		
		cerrar();
		if(e!=null){
			throw e;
		}
		return lista;
	}

	@Override
	public int crear(Respuesta o) throws Exception {
		// TODO Auto-generated method stub
		e = null;
		db = util.getWritableDatabase();
		db.beginTransaction();
		
		cv = new ContentValues();
		cv.put("formato_id", o.getFormato_id());
		cv.put("ape_paterno", o.getApe_paterno());
		cv.put("ape_materno", o.getApe_materno());
		cv.put("nombre", o.getNombre());
		cv.put("dni", o.getDni());
		cv.put("ape_paterno2", o.getApe_paterno2());
		cv.put("ape_materno2", o.getApe_materno2());
		cv.put("nombre2", o.getNombre2());
		cv.put("dni2", o.getDni2());
		cv.put("P01", o.getP01());
		cv.put("P02", o.getP02());
		cv.put("P03", o.getP03());
		cv.put("P04", o.getP04());
		cv.put("P05", o.getP05());
		cv.put("P06", o.getP06());
		cv.put("P07", o.getP07());
		cv.put("P08", o.getP08());
		cv.put("P09", o.getP09());
		cv.put("P10", o.getP10());
		cv.put("nota", o.getNota());

		int id = (int) db.insert("respuesta", null, cv);
		
		if (id != -1) {
			o.setRespuesta_id(id);
			db.setTransactionSuccessful();
		} else {
			e = new Exception("INSERT: error en el dao");
		}

		cerrar();
		if (e != null) {
			throw e;
		}
		
		return id;
	}
	
	public void cerrar(){
		if(c!=null){
			c.close();
		}
		if(cv != null){
			cv.clear();
		}
		if(db.inTransaction()){
			db.endTransaction();
		}
		db.close();
	}

	@Override
	public int eliminar(Respuesta o) throws Exception {
		// TODO Auto-generated method stub
		e=null;
		db = util.getWritableDatabase();
		db.beginTransaction();
		
		int cont = db.delete("respuesta", "respuesta_id=?", new String[]{""+o.getRespuesta_id()});
		if(cont>0){
			db.setTransactionSuccessful();
		}else{
			e = new Exception("DELETE: error en el DAO");
		}
		
		if(e!=null){
			throw e;
		}
		cerrar();
		return cont;
	}

	@Override
	public int actualizar(Respuesta o) throws Exception {
		// TODO Auto-generated method stub
		e = null;
		db = util.getWritableDatabase();
		db.beginTransaction();
		
		cv = new ContentValues();
		cv.put("ape_paterno", o.getApe_paterno());
		cv.put("ape_materno", o.getApe_materno());
		cv.put("nombre", o.getNombre());
		cv.put("dni", o.getDni());
		cv.put("ape_paterno2", o.getApe_paterno2());
		cv.put("ape_materno2", o.getApe_materno2());
		cv.put("nombre2", o.getNombre2());
		cv.put("dni2", o.getDni2());
		cv.put("P01", o.getP01());
		cv.put("P02", o.getP02());
		cv.put("P03", o.getP03());
		cv.put("P04", o.getP04());
		cv.put("P05", o.getP05());
		cv.put("P06", o.getP06());
		cv.put("P07", o.getP07());
		cv.put("P08", o.getP08());
		cv.put("P09", o.getP09());
		cv.put("P10", o.getP10());
		cv.put("nota", o.getNota());

		int ind = (int) db.update("respuesta", cv, "respuesta_id=?", new String[]{""+o.getRespuesta_id()});
		
		if (ind == 1) {
			db.setTransactionSuccessful();
		} else if(ind>1){
			e = new Exception("UPDATE: actualizo mas registros de lo debido");
		}else {
			e = new Exception("UPDATE: error en el DAO");
		}

		cerrar();
		if (e != null) {
			throw e;
		}
		
		return ind;
	}

}
